package com.panda_coder.liedetector;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.data.distributed.common.*;
import ohos.data.distributed.user.SingleKvStore;
import ohos.rpc.*;
import ohos.utils.zson.ZSONObject;

import java.util.HashMap;
import java.util.Map;

import com.panda_coder.liedetector.utils.AsrUtils;

public class AnswerAbility extends Ability {
    AnswerRemote remote = new AnswerRemote();
    KvManagerConfig config;
    KvManager kvManager;
    private static final String STORE_ID = "distributed_db";
    private SingleKvStore singleKvStore;
//    AsrUtils asrUtils;
    private static HashMap<Integer, IRemoteObject> remoteObjectHandlers = new HashMap<Integer, IRemoteObject>();


    private SingleKvStore createDb() {
        SingleKvStore kvstore = null;
        try {
            Options options = new Options();
            options.setCreateIfMissing(true).setEncrypt(false).setKvStoreType(KvStoreType.SINGLE_VERSION);
            kvstore = kvManager.getKvStore(options, STORE_ID);
        } catch (KvStoreException exception) {

        }
        return kvstore;
    }

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        try{
            config = new KvManagerConfig(this.getContext());
            kvManager = KvManagerFactory.getInstance().createKvManager(config);
            singleKvStore = createDb();
        }catch (KvStoreException exception){

        }
    }

    @Override
    public void onStop() {
        super.onStop();
    }


    @Override
    public IRemoteObject onConnect(Intent intent) {
        super.onConnect(intent);
        return remote.asObject();
    }

    class AnswerRemote extends RemoteObject implements IRemoteBroker {
        AnswerRemote() {
            super("");
        }

        @Override
        public boolean onRemoteRequest(int code, MessageParcel data, MessageParcel reply, MessageOption option) {
            Map<String, Object> zsonResult = new HashMap<String, Object>();
            String zsonStr = data.readString();
            ZSONObject zson = ZSONObject.stringToZSON(zsonStr);
            switch (code) {
                case 100: {
                   String result= singleKvStore.getString("before_heart");
                   zson.put("result",result);
//                    asrUtils.start();
                    break;
                }
                case 101: {
//                    asrUtils.stop();
                    break;
                }
                case 200: {
                    remoteObjectHandlers.put(200, data.readRemoteObject());
                    singleKvStore.delete("before_heart");
                    singleKvStore.delete("after_heart");
                    getAsrText();
                    break;
                }
                default: {
                    reply.writeString("service not defined");
                    return false;
                }
            }
            reply.writeString(ZSONObject.toZSONString(zsonResult));
            return true;
        }

        @Override
        public IRemoteObject asObject() {
            return this;
        }
    }

    public void getAsrText() {
        new Thread(() -> {
            while (true) {
                try {
                    Thread.sleep(1 * 500);
                    Map<String, Object> zsonResult = new HashMap<String, Object>();
//                    zsonResult.put("text", asrUtils.getResultAndClear());
                    try{
                        String heart = singleKvStore.getString("before_heart");
                        zsonResult.put("before_heart", heart);
                    }catch (Exception ex){
                        zsonResult.put("before_heart", null);
                    }
                    try{
                        String afterHeart=singleKvStore.getString("after_heart");
                        zsonResult.put("after_heart",afterHeart);

                    }catch(Exception ex){
                        zsonResult.put("after_heart", null);
                    }
                    ReportEvent(200, zsonResult);
                } catch (RemoteException | InterruptedException e) {
                    break;
                }
            }
        }).start();
    }

    private void ReportEvent(int remoteHandler, Object backData) throws RemoteException {
        MessageParcel data = MessageParcel.obtain();
        MessageParcel reply = MessageParcel.obtain();
        MessageOption option = new MessageOption();
        data.writeString(ZSONObject.toZSONString(backData));
        IRemoteObject remoteObject = remoteObjectHandlers.get(remoteHandler);
        remoteObject.sendRequest(100, data, reply, option);
        reply.reclaim();
        data.reclaim();
    }


}
